<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/Action.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/Response.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/Service.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/entities/Person.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Error.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Pattern.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/utils/Str.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/ServiceFactory.php';

final class PersonPasswordSetAction extends Action {
	protected function accessIdentities() {
		return null;
	}

	protected function execute(Response $response) {
		if(isset($_SESSION['user'])) {
			$person = unserialize($_SESSION['user']);
			if(isset($_POST['passwordold']) && isset($_POST['password'])) {
				if($person->getPassword() === md5($_POST['passwordold'])) {
					$person->setPassword(md5($_POST['password']));
					$serviceFactory = new ServiceFactory();// Создаем объект фабрики сервисов
					$service = $serviceFactory->getService('PersonService');// Получаем нужный сервис
					$service->changePassword($person);// Выполняем дейтсвие
					$_SESSION['user'] = serialize($person);
				} else {
					$_SESSION['error'] = 'Старый пароль введен неверно';
				}
			} else {
				$_SESSION['error'] = 'Старый или новый пароль не заполнены';
			}
			$response->setActionPath('/person/editPrivate.html');
		} else {
			$response->setActionPath('/login.html');
		}
	}
}
?>